/**
 * Notes: 预约后台管理
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY www.code3721.com
 * Date: 2022-12-08 07:48:00 
 */

const BaseAdminService = require('./base_admin_service.js');
const AdminMeetService = require('./admin_meet_service.js');
const timeUtil = require('../../../framework/utils/time_util.js');

const MeetModel = require('../../model/meet_model.js');
const JoinModel = require('../../model/join_model.js');
const UserModel = require('../../model/user_model.js');
const DayModel =  require('../../model/day_model.js');

const DataService = require('./../data_service');

// 导出报名数据KEY
const EXPORT_JOIN_DATA_KEY = 'join_data';

// 导出用户数据KEY
const EXPORT_USER_DATA_KEY = 'user_data';

class AdminExportService extends BaseAdminService {
	// #####################导出报名数据
	/**获取报名数据 */
	async getJoinDataURL() {
		let dataService = new DataService();
		return await dataService.getExportDataURL(EXPORT_JOIN_DATA_KEY);
	}

	/**删除报名数据 */
	async deleteJoinDataExcel() {
		let dataService = new DataService();
		return await dataService.deleteDataExcel(EXPORT_JOIN_DATA_KEY);
	}

	// 根据表单提取数据
	_getValByForm(arr, mark, title) {
		for (let k in arr) {
			if (arr[k].mark == mark) return arr[k].val;
			if (arr[k].title == title) return arr[k].val;
		}

		return '';
	}

	/**导出报名数据 */
	async exportJoinDataExcel({
		meetId,
		startDay,
		endDay,
		status
	}) {
        let whereMeet = {
            _id: meetId
        }
        let meetData = await MeetModel.getOne(whereMeet);
        let meetTitle = meetData.MEET_TITLE;
        let where = {
            DAY_MEET_ID: meetId,
            day: ['between', startDay, endDay]
        }
        let orderBy = {
            day: 'asc'
        }
        let row = ['预约名称', '预约日期','时间段','是否签到'] ;// 表属性
        let alldata = [];
        let daySet = await DayModel.getAllBig(where, 'day,times,dayDesc', orderBy); // 时段
		for(let i in daySet){

		   let times =  daySet[i].times;
		   let fields = '*';
		   let head=0;
		   for(let j in times){
               let where = {
                   JOIN_MEET_ID: meetId,
                   JOIN_MEET_TIME_MARK: times[j].mark,
                   JOIN_STATUS : status
               };
               let  res = await JoinModel.getList(where, fields);
               let joinData =  res.list;
               // 将数据添加到alldata中
               for(let key in joinData) {
                   let arr = []
                   arr.push(meetTitle);
                   arr.push(daySet[i].day);
                   arr.push(times[j].start+'～'+times[j].end);
                   let statusDesc = JoinModel.getDesc('STATUS', joinData[key].JOIN_STATUS);
                   if(joinData[key].JOIN_IS_CHECKIN==1){
                       statusDesc = statusDesc+ '已签到';
                   }else{
                       statusDesc = statusDesc+ '未签到';
                   }
                   arr.push(statusDesc);
                   for(let form in joinData[key].JOIN_FORMS){
                       if(key == 0 && head == 0){
                           row.push(joinData[key].JOIN_FORMS[form].title);
                       }
                       arr.push(joinData[key].JOIN_FORMS[form].val);
                   }
                   if(head == 0){
                       alldata.push(row);
                       head = head+1;
                   }
                   alldata.push(arr);
               }
           }
		}
        let dataService = new DataService();
		let length = 0;
		if(alldata.length>0){
            length = alldata.length-1;
        }
        return await dataService.exportDataExcel(EXPORT_JOIN_DATA_KEY, "预约名单", length, alldata);
	}


	// #####################导出用户数据

	/**获取用户数据 */
	async getUserDataURL() {
		let dataService = new DataService();
		return await dataService.getExportDataURL(EXPORT_USER_DATA_KEY);
	}

	/**删除用户数据 */
	async deleteUserDataExcel() {
		let dataService = new DataService();
		return await dataService.deleteDataExcel(EXPORT_USER_DATA_KEY);
	}

	/**导出用户数据 */
	async exportUserDataExcel(condition) {
        let where = JSON.parse(decodeURIComponent(condition));
        let dataService = new DataService();
        let fields = '*';
        let res = await  UserModel.getList(where,fields);
        let data =  res.list;
        let row = ['姓名', '手机','城市','行业','单位','注册时间'] ;// 表属性
        let alldata = [];
        alldata.push(row)

        // 将数据添加到alldata中
        for(let key in data) {
            let arr = []
            arr.push(data[key].USER_NAME);
            arr.push(data[key].USER_MOBILE);
            arr.push(data[key].USER_CITY);
            arr.push(data[key].USER_TRADE);
            arr.push(data[key].USER_WORK);
            arr.push(timeUtil.timestamp2Time(data[key].USER_ADD_TIME));
            alldata.push(arr);
        }
        return await dataService.exportDataExcel(EXPORT_USER_DATA_KEY, "用户资料", data.length, alldata);
	}
}

module.exports = AdminExportService;